//import winterwell.jtwitter.Twitter;
//// Make a Twitter object
//Twitter twitter = new Twitter("Username","password");
//// Print Daniel Winterstein's status
////System.out.println(twitter.getFollowers().toString());
//System.out.println(twitter.getFollowers().get(0).getScreenName());
//// Set my status
////twitter.setStatus("ss!");
////	System.out.println(twitter.sendMessage("username", "password"));
//System.out.println(twitter.sendMessage(twitter.getFollowers().get(0).getScreenName(), "sending " + twitter.getFollowers().get(0).getScreenName().toString()));
/**
 * 
 */

//import twitter4j.DirectMessage;
//import twitter4j.Twitter;
//import twitter4j.TwitterException;
//	Twitter twitter = new Twitter("username", "password");
//		 try {
//	            DirectMessage message = twitter.sendDirectMessage("username", "hi, twitter4j");
//	            System.out.println("Direct message successfully sent to " +
//	                               message.getRecipientScreenName());
//	            System.exit(0);
//	        } catch (TwitterException te) {
//	            System.out.println("Failed to send message: " + te.getMessage());
//	            System.exit( -1);
//	        }
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author innovationstartups
 *
 */
public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String pin = null;
        try {
            Twitter twitter = new Twitter();
            // Must supply consumer key, consumer secret by registering the app on twitter
            twitter.setOAuthConsumer("Real consumer key", "real consumer secret");
            RequestToken requestToken = twitter.getOAuthRequestToken();
            System.out.println("Got request token.");
            System.out.println("Request token: "+ requestToken.getToken());
            System.out.println("Request token secret: "+ requestToken.getTokenSecret());
            AccessToken accessToken = null;

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            while (null == accessToken) {
                System.out.println("Open the following URL and grant access to your account:");
                System.out.println(requestToken.getAuthorizationURL());
                System.out.print("Hit enter when it's done.[Enter]:");
              pin =  br.readLine();
                
                try{
                   // accessToken = requestToken.getAccessToken();
                	System.out.print("checking PIN with twitter....");
              
                	accessToken = twitter.getOAuthAccessToken(requestToken, pin);
                	
                } catch (TwitterException te) {
                    if(401 == te.getStatusCode()){
                        System.out.println("Unable to get the access token.");
                    }else{
                        te.printStackTrace();
                    }
                }
            }
            System.out.println("Got access token.");
            // Must save it somewhere for future access
            System.out.println("Access token: "+ accessToken.getToken());
            // Must save it somewhere for future access
            System.out.println("Access token secret: "+ accessToken.getTokenSecret());

            Status status = twitter.updateStatus("twitter4j OAuth working");
            System.out.println("Successfully updated the status to [" + status.getText() + "].");
            System.exit(0);
        } catch (TwitterException te) {
            System.out.println("Failed to get timeline: " + te.getMessage());
            System.exit( -1);
        } catch (IOException ioe) {
            System.out.println("Failed to read the system input.");
            System.exit( -1);
        }

	}
}
